package com.learn.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.learn.shop.pojo.entriy.NovelChapterMongo;
import com.learn.shop.pojo.vo.CrawlingNovelVo;
import com.learn.shop.service.CrawlingNovelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.stream.Collectors;

@Service("CrawlingNovelImplV2")
@Slf4j
public class CrawlingNovelImplV2 implements CrawlingNovelService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public synchronized String novel(CrawlingNovelVo crawlingNovelV) throws IOException {
        String url = crawlingNovelV.getUrl();
        String novelName = crawlingNovelV.getNovelName();
        String urlSplit = crawlingNovelV.getUrlSplit();

        String parentUrl = "";
        if (StringUtils.isNotBlank(urlSplit)) {
            parentUrl = url.split(urlSplit)[0] + urlSplit;
        } else {
            parentUrl = url;
        }

        int nThreads = 6;
        ExecutorService executorService = Executors.newFixedThreadPool(nThreads);

//        try {
        Document doc = Jsoup.connect(url).get();
        Elements list = Objects.requireNonNull(doc.getElementById("list")).select("dd>a");


        //skip扔掉集合的前n个元素
//            List<Element> collect = list.stream().skip(9).collect(Collectors.toList());
        int size = list.size();

//        List<Future> futures = new ArrayList<>(nThreads);
        for (int i = 0; i < nThreads; i++) {
            int total = size % nThreads == 0 ? size / nThreads : (size / nThreads + 1);
            List<Element> arrList = new ArrayList<>();
            if (i == nThreads - 1) {
                arrList = list.subList(total * i, size);
            } else {
                arrList = list.subList(total * i, total * (i + 1));
            }
            int resI = i;
            List<Element> resList = arrList;
//            log.error("分割后的list集合--->" + resList);
            String finalParentUrl = parentUrl;
            Runnable task = () -> {
                log.info("使用线程池分批次解析HTML ： " + Thread.currentThread().getName() +
                        "  index:: " + total * resI + "  size:: " + resList.size());
                for (int j = 0; j < resList.size(); j++) {
                    Element element = list.get(j);
                    String childUrl = element.attr("href");
                    String title = element.text();
                    log.info(title);
                    getNovelContent(title, finalParentUrl, childUrl, novelName);
                }
            };
            executorService.submit(task);
//            futures.add(executorService.submit(task));

        }
        while (true) {
            //如果线程全部执行完毕
            if (executorService.isTerminated()) {
                executorService.shutdown();
                //更新推送状态
                return "解析完毕!";
            }
        }
//        } catch (IOException e) {
//            e.printStackTrace();
//            return "解析异常!";
//        }

    }

    public void getNovelContent(String title, String parentUrl, String childUrl, String novelName) {

        try {
            Document document = Jsoup.connect(parentUrl + childUrl)
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .header("Cache-Control", "no-cache")
                    .header("Connection", "keep-alive")
                    .header("Pragma", "no-cache")
                    .header("Upgrade-Insecure-Requests", "1")
                    .timeout(8000)
                    .userAgent("Mozilla")//模拟浏览器
                    .get();
            String content = Objects.requireNonNull(document.getElementById("content")).html();
            content = content.replace("<br>", "");
            content = content.replace("&nbsp;", "");
            content = content.replace("无广告手机站: m.shengxu5w.com 同步更新《万古神帝》小说", "");
            NovelChapterMongo mongo = new NovelChapterMongo();
            mongo.set_id(UUID.randomUUID().toString());
            mongo.setChapterName(title);
            mongo.setText(content);
            mongoTemplate.save(mongo, novelName);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("解析小说失败!");
        }
    }
}
